package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import test0.dao.factory.DAOfactory;
import test0.dao.vo.User;

public class LoginServlet extends HttpServlet {
	public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
		String path="login.jsp";
		String userid=req.getParameter("userid");
		String password=req.getParameter("password");
		List<String> info=new ArrayList<String>();
		if(userid==null|"".equals(userid)){
			info.add("用户id不能为空！");
		}
		if(password==null|"".equals(password)){
			info.add("用户密码不能为空！");
		}
		if(info.size()==0){
		 	User user=new User();
			user.setUserid(userid);
			user.setPassword(password);
			try{
				if(DAOfactory.getIUserDAOInstance().findLogin(user)){
		info.add("用户登录成功，欢迎："+user.getName());
				}else{
		info.add("用户登录失败，用户id或密码错误。");
		}
			}catch(Exception e){
				e.printStackTrace();
			}
			
			
		}
			

	req.setAttribute("info",info);
	req.getRequestDispatcher(path).forward(req,resp);
	}

	public void doPost(HttpServletRequest req,HttpServletResponse resp)throws  ServletException,IOException{
	this.doGet(req,resp);
	}

}
